<template>
  <div v-loading="isLoading" class="blogContent-container" ref="container">
    <ul>
      <li v-for="item in banners.rows" :key="item.id">
        <div class="img" v-if="item.imgsrc">
          <router-link :to="{name:'BlogBody',params:{id:item.id}}">
            <img v-lazying="item.imgsrc" :alt="item.title" :title="item.title" />
          </router-link>
        </div>
        <div class="main">
          <router-link :to="{name:'BlogBody',params:{id:item.id}}">
            <h2 class="title">{{ item.title }}</h2>
          </router-link>

          <div class="information">
            <span>日期：{{ getDate(item.createDate) }}</span>
            <span>浏览：{{ item.scanNumber }}</span>
            <span>评论：{{ item.commentNumber }}</span>
          </div>
          <div class="desc">
            {{ item.description }}
          </div>
        </div>
      </li>
    </ul>
    <Pager
      @pageChange="handleChange"
      :current="getRoute.page"
      :total="banners.total"
      :limit="10"
    />
  </div>
</template>

<script>
import getBlogContent from "@/api/getBlogContent";
import Pager from "@/components/Pager";
import getDate from "@/utils/getdate";
export default {
  data() {
    return {
      banners: [],
      isLoading: true,
    };
  },
  components: {
    Pager,
  },
  methods: {
    getDate,
    handleChange(newpage) {
      if (this.getRoute.categoryId == -1) {
        // 无刷新跳转
        this.$router.push({
          name: "Blog",
          query: {
            page: newpage,
          },
        });
      } else {
        this.$router.push({
          name: "categoryId",
          params: {
            categoryId: this.getRoute.categoryId,
          },
          query: {
            page: newpage,
          },
        });
      }
    },
     handleScroll() {
      this.$bus.$emit("blogScroll", this.$refs.container);
    },
  },
  async created() {
    this.banners = await getBlogContent(
      this.getRoute.page,
      this.getRoute.limit,
      this.getRoute.categoryId
    );
    this.isLoading = false;
  },
    mounted() {
    this.$refs.container.addEventListener("scroll", this.handleScroll);
    },
  computed: {
    // 获取导航路由信息
    getRoute() {
      // 获取路由id 当前页，页容量
      const categoryId = +this.$route.params.categoryId || -1;
      const page = +this.$route.query.page || 1;
      const limit = +this.$route.query.limit || 10;
      return { categoryId, page, limit };
    },
  },
  //监听数据变化
  watch: {
    //监听地址路由变化
    async $route() {
      //地址路由变化，重新获取数据
      this.isLoading = true;
      this.$refs.container.scrollTop = 0;
      this.banners = await getBlogContent();
      this.isLoading = false;
    },
  },
};
</script>
<style lang="less" scoped>
* {
  margin: 0;
  padding: 0;
  list-style: none;
  text-decoration: none;
  color: inherit;
}
.blogContent-container {
  position: relative;
  width: 100%;
  height: 100%;
  padding: 15px;
  line-height: 1.7;
  overflow-y: auto;
  box-sizing: border-box;
  //平滑滚动搭配scroll
  scroll-behavior: smooth;
  li {
    display: flex;
    padding: 15px 0;
    border-bottom: 1px solid gray;
    .img {
      flex: 0 0 auto;
      margin-right: 15px;
      img {
        display: block;
        max-width: 200px;
        border-radius: 5px;
      }
    }
    .main {
      flex: 1 1 auto;
      .information {
        font-size: 12px;
        color: gray;
        span {
          margin-right: 15px;
        }
      }
      .desc {
        margin: 15px 0;
        font-size: 14px;
      }
      .title:hover,
      .desc:hover {
        color: red;
        text-decoration: underline;
      }
    }
  }
}
</style>